import { ref } from 'vue'
import { defineStore } from 'pinia'
import requests from '../axios/request.js'
import { showConfirmDialog } from 'vant';
export const useCounterStore = defineStore('counter', () => {
  

  //水果页面请求数据
  const fruitdata = ref()
  const fruit = function () {
    requests({
      url: '/mock/fruitdata',
      method: 'get'
    }).then(res => {
      fruitdata.value = res.data.data
    })
  }
  fruit();


  // 早餐页面请求数据
  const morningdata = ref()
  const morning = function () {
    requests({
      url: '/mock/morningdata',
      method: 'get'
    }).then(res => {
      morningdata.value = res.data.data
    })
  }
  morning();



  //休闲页面请求数据
  const leisuredata = ref()
  const leisure = function () {
    requests({
      url: '/mock/leisuredata',
      method: 'get'
    }).then(res => {
      leisuredata.value = res.data.data
    })
  }
  leisure();


// 全部的请求
  const commoditydata = ref()
  const commodity = function () {
    requests({
      url: '/mock/commoditydata',
      method: 'get'
    }).then(res => {
      commoditydata.value = res.data.data
    })
  }
  commodity();
  


  //人气
   const hotdata = ref()
   const hot = function () {
    requests({
      url: '/mock/hotdata',
      method: 'get'
    }).then(res => {
      hotdata.value = res.data.data
    })
  }
  hot();



  //购物车数据
  // const cardata = ref()
  // const car = function () {
  //   requests({
  //     url: '/mock/cardata',
  //     method: 'get'
  //   }).then(res => {
  //     cardata.value = res.data.data
  //   })
  // }
  // car();

  
  const cardata = ref([
    {
      id:1,
      price:10,
      name:"小龙虾",
      img:'https://img.zcool.cn/community/0194b45b599ae4a801215c8fa2c9e9.jpg@1280w_1l_2o_100sh.jpg',
      origin: "山东",
      desc: "山东油条",
      count: 1,
      detailed: "https://vpic.cms.qq.com/nj_vpic/2748174398/1625401338648806100",
      mark: false,
      inventory: 120
    }
    ,
    {
      id:2,
      price:50,
      name:"顶级葡萄酒",
      img:'https://img.zcool.cn/community/01fb655db536e5a8012163ba9cd471.jpg@1280w_1l_2o_100sh.jpg',
      origin: "山东",
      desc: "山东油条",
      count: 1,
      detailed: "https://vpic.cms.qq.com/nj_vpic/2748174398/1625401338648806100",
      mark: false,
      inventory: 120
    },
    {
      id:3,
      price:1,
      name:"鸡蛋",
      img:'https://img.zcool.cn/community/01f4e75c08b576a80120925200ca38.jpg@2o.jpg',
      origin: "山东",
      desc: "山东油条",
      count: 1,
      detailed: "https://vpic.cms.qq.com/nj_vpic/2748174398/1625401338648806100",
      mark: false,
      inventory: 120
    }
  ])

  const messagedata = ref([
    {
      id:1,
      img:"https://ts1.tc.mm.bing.net/th/id/R-C.6eee304a29095c5d3047bd59427f8a5b?rik=qKIkdZ75VYJQmQ&riu=http%3a%2f%2fx0.ifengimg.com%2fres%2f2019%2f8C23ED6604FBEE84E69F8DCF67243BE07AE9CD6F_size63_w894_h894.jpeg&ehk=wvr2lI%2b9fS%2f4wrApq%2ftbZbaCl8MGGkhc5TssSB28qFw%3d&risl=&pid=ImgRaw&r=0",
      name:"旺旺食品旗舰店",
      date:'2024-06-25',
      message:'旺仔牛奶糖最近特价'
    }
    ,
    {
      id:2,
      img:"https://bpic.588ku.com/back_origin_min_pic/19/10/22/3816801f1f140fac5d994bc4b0de1be4.jpg!/fw/750/quality/99/unsharp/true/compress/true",
      name:"百果园水果旗舰店",
      date:'2024-06-26',
      message:'欢迎购物新苹果'
    },
    {
      id:3,
      img:'https://tse2-mm.cn.bing.net/th/id/OIP-C.4rFCsUZgUq-B6dnE_MuglAHaHZ?rs=1&pid=ImgDetMain',
      name:"coco食品旗舰店",
      date:'2024-06-28',
      message:'亲'
    },
    {
      id:4,
      img:'https://tse2-mm.cn.bing.net/th/id/OIP-C.wGSb-PL8CwNVw52s7AuoswHaFj?rs=1&pid=ImgDetMain',
      name:"88上服装旗舰店",
      date:'2024-06-30',
      message:'服装全场9折'
    }
  ])

  
  // 遍历cardata，price相加
  const total = ref(0)
  const totalPrice = function () {
    total.value = 0;
    for (let i = 0; i < cardata.value.length; i++) {
        if (cardata.value[i].mark) {
          total.value += cardata.value[i].price * cardata.value[i].count
        }
    }
    return total.value
  }
  totalPrice();

  //删除购物车商品
  const deleteGoods = function (id) {
    showConfirmDialog({
      title: '温馨提示',
      message:
        `你确定要把我从购物车中删除吗？`,
    })
      .then(() => {
        for (let i = 0; i < cardata.value.length; i++) {
          if (cardata.value[i].id === id) {
            cardata.value.splice(i, 1)
          }
        }
        totalPrice();
      });
  
  }



  //获取角色数据，用于账号密码的登录
  const userdata = ref()
  const user = function () {
   requests({
     url: '/mock/user',
     method: 'get'
   }).then(res => {
    console.log(res)
     userdata.value = res.data.data
   })
 }
 user();



  //检查是否登录成功
  const setIsLogin = ref(false) 
  //检查是哪个用户登录成功
  const loginid = ref(100);
 //登录注册的切换页面
 const enroll = ref(false)
  return { 
           commoditydata,
           fruitdata,
           morningdata,
           leisuredata,
           hotdata,
           cardata,
           total,
           totalPrice,
           deleteGoods,
           userdata,
           setIsLogin,
           loginid,
           messagedata,
           enroll,

  }
})
